<?php 

/**
 * 茉莉QQ机器人安装向导
 * @author 冬天的秘密
 * @link http://bbs.itpk.cn
 * @version 1.0
 */

/**
 * Webqq安装根目录
 * @var string
 */
define('WEB_ROOT', "../");

/**
 * 定义一个常量，用来防止别人直接访问程序内部文件
 */
define('ITPK', 'ITPK');

require_once WEB_ROOT . 'class/tools/DataUtil.php';
require_once WEB_ROOT . 'class/tools/ErrorUtil.php';

$config_file = WEB_ROOT . 'db-config.php';

if (defined('SAE_ACCESSKEY')) {
	ErrorUtil::put("暂不支持SAE的安装：\r\n由于新浪SAE关闭了IO的写入，导致插件安装不能正常执行。\r\n以后可能会发布新浪SAE专版，非常抱歉！");
} elseif (file_exists($config_file) && !DataUtil::is_empty(file_get_contents($config_file))) {
	/**
	 * 如果存在db-config.php文件，并且内容不为空，则提示已经安装成功
	 */
	ErrorUtil::put("您已经安装成功，重新安装请清空db-config.php文件的内容！");
} elseif (!file_exists($config_file)) {
	/**
	 * 如果不存在db-config.php文件则给出提示
	 */
	ErrorUtil::put("缺失db-config.php文件，请上传（重写安装请直接上传空文件）。");
}

$step = DataUtil::param_mysql_filter("step", 1);
$step = is_numeric($step) ? $step : 1;

switch($step) {
	case 1:
		$is_support_curl = function_exists('curl_init');
		$is_support_mysqli = class_exists('mysqli');
		$is_support_zipArchive = class_exists('ZipArchive');
		$is_support_config_writable = is_writable($config_file);
		$is_support_plugin_writable = is_writable(WEB_ROOT . "plugin");
		$php_version = explode('-', phpversion());
        $php_version = $php_version[0];
        $php_version_ge523 = strnatcasecmp($php_version, '5.2.3') >= 0 ? true : false;
		$is_support_step = ($is_support_curl && $is_support_mysqli && $is_support_zipArchive && $is_support_config_writable && $is_support_plugin_writable && $php_version_ge523) ? true : false;
		break;
	case 2:
		$tip = DataUtil::param_mysql_filter("tip");
		break;
	case 3:
		$db_host = DataUtil::param_mysql_filter("db_host");
		$db_user = DataUtil::param_mysql_filter("db_user");
		$db_pass = DataUtil::param_mysql_filter("db_pass");
		$db_base = DataUtil::param_mysql_filter("db_base");
		$db_port = DataUtil::param_mysql_filter("db_port");
		if (DataUtil::is_empty($db_host) || DataUtil::is_empty($db_user) || DataUtil::is_empty($db_pass) || DataUtil::is_empty($db_base) || DataUtil::is_empty($db_port)) {
			$step = 2;
			$tip = "请填写完数据库配置";
			break;
		}
		$username = DataUtil::param_mysql_filter("username");
		$password = DataUtil::param_mysql_filter("password");
		if (DataUtil::is_empty($username) || DataUtil::is_empty($password)) {
			$step = 2;
			$tip = "请填写完管理员设置";
			break;
		}
		$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_base, $db_port);
		$mysqli->set_charset("utf8");
		if (mysqli_connect_errno()) {
			$step = 2;
			$tip = "数据库连接出错，请检查数据库配置是否正确";
			//$tip = mysqli_connect_error();	//如果你需要知道具体的错误原因可以把此注释取消
			break;
		}
		$db_config_content = "<?php if (!defined('ITPK')) exit('You can not directly access the file.');\r\n\r\n/**\r\n * 数据库配置文件\r\n * @author 冬天的秘密\r\n * @link http://bbs.itpk.cn\r\n * @version 1.0\r\n */\r\n\r\n/**\r\n * 数据库配置（编码统一为UTF-8）\r\n * DBHOST	数据库主机\r\n * DBUSER	数据库用户名\r\n * DBPASS	数据库密码\r\n * DBBASE	数据库名字\r\n * DBPORT	数据库端口\r\n * DBCODE	数据库编码\r\n */\r\ndefine('DBHOST', '{$db_host}');\r\ndefine('DBUSER', '{$db_user}');\r\ndefine('DBPASS', '{$db_pass}');\r\ndefine('DBBASE', '{$db_base}');\r\ndefine('DBPORT', '{$db_port}');\r\ndefine('DBCODE', 'utf8');\r\n\r\n?>";
		if (!@file_put_contents($config_file, $db_config_content)) {
			$step = 2;
			$tip = "数据库配置写入db-config.php文件失败!";
			break;
		}
		if (!file_exists("web.sql")) ErrorUtil::put("缺少程序安装文件（install/web.sql）！");
		$sql = file_get_contents("web.sql");
		$mysqli->multi_query($sql . " INSERT INTO web_user(username, password, user_check, createdate) VALUES('{$username}', '" . md5($password) . "', '', " . time() . ");");
		break;
	default:
		break;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="description" content="茉莉QQ机器人，新一代智能机器人。" />
<meta name="keywords" content="茉莉QQ机器人,QQ机器人" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0" />
<title>茉莉QQ机器人 - 安装向导</title>
<link rel = "stylesheet" href = "http://libs.useso.com/js/font-awesome/4.2.0/css/font-awesome.min.css" />
<style type = "text/css">
::selection{color:#FFF;background:#70CEE9}::-moz-selection{color:#FFF;background:#70CEE9;}
html, body, div, table, th, tr, td, h1, h2, h3, h4, h5, h6, ol, ul, li, input, textarea, header, center, small, label, span, p, hr, a, img {margin:0px;padding:0px;border:0px;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;}
body{width:100%;height:100%;font-family:Helvetica, "Microsoft Yahei", Arial, sans-serif;font-size:14px;line-height:1.42857143;color:#555;position:relative;max-width:460px;margin:auto;}
*:focus {outline:none;} *:before, *:after{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}
h1, h2, h3, h4, h5, h6{font-family:inherit;font-weight:500;line-height:1.1;color:inherit;}
input, a, small{-webkit-transition: all 0.2s;-moz-transition: all 0.2s;-ms-transition: all 0.2s;-o-transition: all 0.2s;transition: all 0.2s;}
.anima a{-webkit-transition: all 0.5s;-moz-transition: all 0.5s;-ms-transition: all 0.5s;-o-transition: all 0.5s;transition: all 0.5s;}
ol, ul {list-style:none;} li {display:list-item;text-align:-webkit-match-parent;}
a {color:#1DA075;text-decoration:none;}
.tip{text-align:center;} .tip-green{color:green;}.tip-red{color:red;}.tip-blue{color:blue;}.tip-color1{color:#7CBB00;}.tip-color2{color:#F65314;}.tip-color3{color:#00A1F1;}
.disabled {background-color:#ccc !important;} .display{display:none;}
.container {position:relative;margin:0px auto;max-width:460px;min-width:320px;clear:both;background:#ffd;padding:20px;}
strong {font-weight:bold;} code {display:inline-block;padding:2px 4px;font-size:90%;background:#f7f7f9;color:green;border-radius:4px;white-space:nowrap;border:1px solid #e1e1e8;}
.zero_ul{list-style:decimal-leading-zero;margin:20px 0 14px 29px;}
.circle_ul{list-style:circle;margin:20px 0 14px 16px;}
.ul_none li, .zero_ul li, .circle_ul li{padding:10px 0px;border-bottom:1px dashed #ddd;}
.step_button{cursor:pointer;display:block;width:100%;line-height:38px;background:#34a782;color:#fff;font-size:16px;text-align:center;}
.page_title{width:70%;text-align:center;font-weight:bold;font-size:18px;margin:0 auto 20px auto;padding-bottom:10px;border-bottom:1px dashed #f60;}
.page_step{padding:10px 0px;}
table{width:100%;}
.t_left{text-align:right;padding-right:20px;}
.t_right{text-align:left;}
th, td{width:50%;border-bottom:1px dashed bisque;padding:6px 0;}
.form_ul li{padding:6px 0px;overflow:hidden;white-space:nowrap;line-height:36px;border-bottom:1px dashed bisque;}
.form_ul label{cursor:pointer;display:inline-block;width:90px;text-align:right;margin-right:6px;}
.form_input{width:180px;height:30px;line-height:30px;font-size:14px;background:transparent;color:#666;padding:0px 5px;border:1px solid #bbb;border-radius:2px;}
.form_button{cursor:pointer;padding:6px 20px;background:#34a782;color:#fff;font-size:16px;}
.bottom_title{padding:10px 0px;margin-bottom:10px;border-bottom:1px dashed #c7e7dc;font-weight:bold;}
</style>
</head>
<body>
<div class = "container">
	<div class = "page_title">茉莉QQ机器人-安装向导</div>
<?php if ($step == 1) { ?>
	<ul class = "ul_none">
	<?php if (getenv('OPENSHIFT_APP_NAME')) { ?>
	<li>检测到为OPENSHIFT环境，请直接进入<a href = "index.php?step=2">下一步</a><br />OPENSHIFT环境暂未测试！</li>
	<?php } else { ?>
	<li><strong>请保证每一个项目都支持，否则不能继续</strong></li>
	<li>
		<table>
			<tr>
				<th class = "t_left">项目</th>
				<th class = "t_right">检测结果</th>
			</tr>
			<tr>
				<td class = "t_left"><code>curl函数</code></td>
				<td class = "t_right"><?php echo $is_support_curl ? "<span class = \"tip-green\">支持<i class = \"fa fa-check fa-fw\"></i></span>" : "<span class = \"tip-red\">不支持<i class = \"fa fa-times fa-fw\"></i></span>";?></td>
			</tr>
			<tr>
				<td class = "t_left"><code>mysqli扩展</code></td>
				<td class = "t_right"><?php echo $is_support_mysqli ? "<span class = \"tip-green\">支持<i class = \"fa fa-check fa-fw\"></i></span>" : "<span class = \"tip-red\">不支持<i class = \"fa fa-times fa-fw\"></i></span>";?></td>
			</tr>
			<tr>
				<td class = "t_left"><code>解压类ZipArchive</code></td>
				<td class = "t_right"><?php echo $is_support_zipArchive ? "<span class = \"tip-green\">支持<i class = \"fa fa-check fa-fw\"></i></span>" : "<span class = \"tip-red\">不支持<i class = \"fa fa-times fa-fw\"></i></span>";?></td>
			</tr>
			<tr>
				<td class = "t_left"><code>db-config.php文件</code></td>
				<td class = "t_right"><?php echo $is_support_config_writable ? "<span class = \"tip-green\">可写<i class = \"fa fa-check fa-fw\"></i></span>" : "<span class = \"tip-red\">不可写<i class = \"fa fa-times fa-fw\"></i></span>";?></td>
			</tr>
			<tr>
				<td class = "t_left"><code>plugin目录</code></td>
				<td class = "t_right"><?php echo $is_support_plugin_writable ? "<span class = \"tip-green\">可写<i class = \"fa fa-check fa-fw\"></i></span>" : "<span class = \"tip-red\">不可写<i class = \"fa fa-times fa-fw\"></i></span>";?></td>
			</tr>
			<tr>
				<td class = "t_left"><code>PHP最低版本5.2.3</code></td>
				<td class = "t_right"><?php echo $php_version_ge523 ? "<span class = \"tip-green\">可用<i class = \"fa fa-check fa-fw\"></i></span>" : "<span class = \"tip-red\">不可用{$php_version}<i class = \"fa fa-times fa-fw\"></i></span>";?></td>
			</tr>
		</table>
	</li>
	</ul>
	<?php if ($is_support_step) { ?>
	<div class = "page_step"><a class = "step_button" href = "index.php?step=2">下一步</a></div>
	<?php } ?>
	<?php } ?>
<?php } elseif ($step == 2) { ?>
	<form action = "index.php?step=3" method = "post">
		<?php if (!DataUtil::is_empty($step)) { ?>
		<div class = "tip tip-red"><?php echo $tip; ?></div>
		<?php } ?>
		<div class = "bottom_title">数据库配置</div>
		<ul class = "form_ul">
			<li><label for = "db_host">数据库主机</label><input class = "form_input" type = "text" id = "db_host" name = "db_host" value = "<?php echo isset($db_host) ? $db_host : "localhost";?>" /></li>
			<li><label for = "db_user">数据库用户名</label><input class = "form_input" type = "text" id = "db_user" name = "db_user" value = "<?php echo isset($db_user) ? $db_user : "";?>" /></li>
			<li><label for = "db_pass">数据库密码</label><input class = "form_input" type = "text" id = "db_pass" name = "db_pass" value = "<?php echo isset($db_pass) ? $db_pass : "";?>" /></li>
			<li><label for = "db_base">数据库名</label><input class = "form_input" type = "text" id = "db_base" name = "db_base" value = "<?php echo isset($db_base) ? $db_base : "";?>" /></li>
			<li><label for = "db_port">数据库端口</label><input class = "form_input" type = "text" id = "db_port" name = "db_port" value = "<?php echo isset($db_port) ? $db_port : "3306";?>" /></li>
		</ul>
		<div class = "bottom_title">设置管理员</div>
		<ul class = "form_ul">
			<li><label for = "username">管理员账号</label><input class = "form_input" type = "text" id = "username" name = "username" value = "<?php echo isset($username) ? $username : "";?>" /></li>
			<li><label for = "password">管理员密码</label><input class = "form_input" type = "text" id = "password" name = "password" value = "<?php echo isset($password) ? $password : "";?>" /></li>
		</ul>
		<div class = "page_step"><input class = "step_button" type = "submit" value = "下一步" /></div>
	</form>
<?php } elseif ($step == 3) { ?>
	<div class = "bottom_title">安装结果</div>
	<ul>
		<li>恭喜您安装成功，请牢记账号和密码</li>
		<li>用户名：<span class = "tip-green"><?php echo $username; ?></span></li>
		<li>密码：<span class = "tip-green"><?php echo $password; ?></span></li>
		<li>前往<a href = "<?php echo WEB_ROOT . "index.php"; ?>">首页</a>或<a href = "<?php echo WEB_ROOT . "login.php"; ?>">登录</a>.</li>
	</ul>
<?php } ?>
</div>
</body>
</html>